﻿module Sustainalytics.Screening.Security {

    import Routing = Sustainalytics.Routing;

    export class ScreeningToolRouteSecurityService implements Routing.IRouteSecurityService {

        constructor(
            private $q: ng.IQService,
            private ScreeningToolPermissionsService: IScreeningToolPermissionsService) {
        }

        validate(security: Routing.RouteSecurity): ng.IPromise<boolean> {
            var ctx = this;

            var valid = false;

            var stPermissions = ctx.ScreeningToolPermissionsService.getScreeningToolPermissions("POST");
            var companyInfoTabPermissions = ctx.ScreeningToolPermissionsService.getScreeningToolCompanyInfoTabPermissions("POST");            
            var esgTabPermissions = ctx.ScreeningToolPermissionsService.getScreeningToolEsgPermissions("POST");

            $.each([stPermissions, companyInfoTabPermissions, esgTabPermissions], (index, permissionsObj) => {

                // get object properties
                for (var prop in permissionsObj) {
                    valid = valid || permissionsObj[prop];

                    if (valid)
                        return false;
                }
            });

            return ctx.$q.when(valid);
        }
    }

    export var screeningToolRouteSecurityService = 'ScreeningToolRouteSecurityService';

    angular.module(screeningToolSecurityModuleName).service(screeningToolRouteSecurityService, ['$q','ScreeningToolPermissionsService', ScreeningToolRouteSecurityService]);
    
}